<template>
	<view>
		<view class="wrap u-p-l-30 u-p-r-30">
			<u-sticky>
				<u-row gutter="1" class="table-title">
					<u-col span="8">
						<view class="demo-layout">商品</view>
					</u-col>
					<u-col span="4">
						<view class="demo-layout u-text-right">库存量</view>
					</u-col>
				</u-row>
			</u-sticky>

			<u-row gutter="1" v-for="(item,index) in inventory" class="table-row">
				<u-col span="8">
					<view class="demo-layout u-line-1">{{item.item_id}} - {{item.item_name}}</view>
					<view class="demo-layout u-font-xs u-line-1">{{item.category_name}}</view>
				</u-col>
				<u-col span="4">
					<view class="demo-layout u-font-xs u-line-1 u-text-right">{{item.stock_num}}</view>
				</u-col>
			</u-row>
			<!-- <u-loadmore class="u-padding-30" :status="loadStatus" @loadmore="loadMore"
				:load-text="loadText" v-if="stores.length>0"></u-loadmore> -->
			<u-back-top :scroll-top="scrollTop"></u-back-top>

		</view>
		<wx-share :title='shop.name' :desc="shop.store_introduction" :imgUrl="shop.cover" link="/" ref="wxs"></wx-share>
	</view>
</template>

<script>
	var jweixin = require('jweixin-module')
	export default {
		data() {
			return {
				scrollTop: 0,
				stores: [],
				inventory:[],
				user: {},
				paging: {
					current_page: 1,
					total_pages: 1,
					page_zie: 100
				},
				loadStatus: 'loadmore',
				loadText: {
					loadmore: '加载更多',
					loading: '努力加载中',
					nomore: '没有更多了'
				},
				shop:{}
			}
		},
		onLoad() {
			this.getShop()
			this.setWxShare()
		},
		onShow() {
			this.getUserInfo();
			this.getInventory();
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onPullDownRefresh(){
			this.paging.current_page = 1
			this.getInventory()
		},
		methods: {
			getShop() {
				this.$u.post('/shopping/shop-setting', {}).then(res => {
					this.shop = res;
				}).catch(e => {

				})
			},
			getUserInfo() {
				this.$u.post('/user/info', {}).then(res => {
					this.user = res;
					if (this.user.referral_id == 0) {
						this.user.referral_id = '';
					}
				}).catch(e => {
					console.log(e)
				})
			},
			getInventory() {
				this.$u.post('/store-manage/get-store-inventory', this.paging).then(res => {
					// if (res.page.current_page === 1) {
					// 	this.stores = res.list;
					// } else {
					// 	this.stores = this.stores.concat(res.list)
					// }
					this.inventory = res
					// this.paging.current_page = res.page.current_page;
					// this.paging.total_pages = res.page.total_pages;
					// this.paging.page_size = res.page.page_size;
					uni.stopPullDownRefresh()
				}).catch(e => {
					console.log(e)
				})
			},
			loadMore() {
				console.log('load more')
				if (this.paging.current_page < this.paging.total_pages) {
					this.paging.current_page = this.paging.current_page + 1;
					this.getInventory();
				}
			},
			openLocation(store){
				console.log(store)
				jweixin.ready(() => {
				  jweixin.openLocation({
				    latitude: parseFloat(store.lat),
				    longitude: parseFloat(store.lng),
				    name: store.shop_name,
					address: store.address,
				    scale: 18,
				    fail: (res) => {
				      console.log(res);
				    }
				  })
				})
			},
			setWxShare() {
				let that = this;
				this.$u.post('/shopping/wx-js-sdk-config', {
					current_page: location.href.split('#')[0]
				}).then(res => {
					console.log(res)
					jweixin.config(res);
					jweixin.ready(function() {
						console.log('wx-is ready')
					});
					jweixin.error(function(res) {
						console.log('wx-is error')
						console.log(res)
						// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
					});
				}).catch(e => {

				})
			}
		}
	}
</script>

<style>
	.demo-layout {
		padding: 10rpx;
		box-sizing: border-box;
	}

	.demo-layout.u-font-xs {
		padding-top: 0;
		color: #8F8F94;
	}
	.demo-layout.green{
		color: #328a44;
	}
	.demo-layout.red{
		color:red;
	}

	.table-title {
		font-weight: 400;
		border-bottom: 1px solid #DDD;
		background-color: #FFF;
		padding: 18rpx 0;
	}

	.table-row {
		border-bottom: 1px dotted #DDD;
	}
</style>
